/**
* Extends fileUploadControllerBase implements ISinglyFileUploadController
* Reference:WaterMark,Sticker...
*/
package com.godpaper.as9.control.upload
{
	import com.godpaper.as9.constants.UploadInfoList;
	import com.godpaper.as9.util.LogHelper;
	import com.godpaper.as9.view.components.UploadProgressBar;
	
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileReference;
	
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	/**
	 * 
	 * @author YangBo
	 * 
	 */
	[Event("singlyFileUploadDataComplete")]
	[Event("singlyFileUploadInProgress")]
	public class SinglyFileUploadController extends FileUploadControllerBase implements ISinglyFileUploadController
	{
		public static const SINGLY_FILE_UPLOAD_IN_PROGRESS:String = "singlyFileUploadInProgress";
		public static const SINGLY_FILE_UPLOAD_DATA_COMPLETE:String = "singlyFileUploadDataComplete";
		private var _browseButton:Object;
		private var _progressBar:UploadProgressBar;
		[Bindable]
		protected var _fileRefer:FileReference;
//--------------------------------------------------------------------------
//
//  Construct
//
//--------------------------------------------------------------------------		
		public function SinglyFileUploadController(
									     method:String="POST",
									     contentType:String="multipart/form-data",
									     target:IEventDispatcher=null)
		{
			//TODO: implement function
			super( method, contentType, target);
			
			updateUIElements(0,null,'browse');
		}
//--------------------------------------------------------------------------
//
//  Properties
//
//--------------------------------------------------------------------------
		//----------------------------------
		//  browseButton
		//----------------------------------			
		public function set browseButton(value:Object):void
		{
			_browseButton = value;
			browseButton.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void
            {
            	browseHandler(event);
            });
		}
		
		public function get browseButton():Object
		{
			return _browseButton;
		}
		//----------------------------------
		//  progressBar
		//----------------------------------			
		public function set progressBar(value:UploadProgressBar):void
		{
			_progressBar = value;
			
			progressBar.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void
            {
            	browseHandler(event);
            });
			
		}
		public function get progressBar():UploadProgressBar
		{
			return _progressBar;
		}
		//----------------------------------
		//  fileRefer
		//----------------------------------			
		public function get fileRefer():FileReference
		{
			return _fileRefer;
		}
		
////////////////////////EventHandler////////////////////////////////////	
	
		override public function browseHandler(event:Event):void
		{
			_fileRefer = new FileReference();
            configureListeners(fileRefer);
			fileRefer.browse(uploadTypes);
		}
		
		override public function selectHandler(event:Event):void
		{
			_fileRefer = FileReference(event.target);
            LogHelper.debug("selectHandler: name=" + fileRefer.name + " URL=" + urlRequest.url);
			urlRequest.data = urlVariables;            
            LogHelper.debug("uploadURLVariables:"+urlRequest.data);  
            urlRequest.contentType = UploadInfoList.CONTENT_TYPE;
            urlRequest.method = UploadInfoList.METHOD_POST;
            LogHelper.debug("uploadURL:"+urlRequest);  
            
            if(!checkFileSize(fileRefer.size))
            {
    			Alert.show(_fileRefer.name + " " + "upload.error.overflowMessage" + 
            			"upload.error.overflowFileInfo" + 
            			Math.round(UploadInfoList.UPLOAD_MAX_FILE_SIZE / 1024) + " KB",
    					"upload.error.overflowTitle",
    					Alert.OK, null, null);
    			
    			updateUIElements(0,null,'browse',0);			
            }
            else
            {
            	fileRefer.upload(urlRequest);
            	dispatchEvent(new Event(SINGLY_FILE_UPLOAD_IN_PROGRESS));
            	updateUIElements(0,fileRefer.name,'upload',0);		
            }
		}
		
		override public function progressHandler(event:ProgressEvent):void
		{
			LogHelper.debug("progressHandler name=" + fileRefer.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
     		
     		CursorManager.setBusyCursor();
        	updateUIElements(event.bytesLoaded,
        					 "uploadProgress",
        					 'upload',
        					 event.bytesTotal,
        					 true,
        					 false);
		}
		
		override public function uploadDataCompleteHandler(event:DataEvent):void
		{
			LogHelper.debug("uploadCompleteData: "+ event);
            CursorManager.removeAllCursors();
            if(event.data.search("<fail>")==-1)
            {
            	updateUIElements(100,
            					 "uploadDone",
            					 "browse");
            	//update watermark image display;				 
           		dispatchEvent(new DataEvent(SINGLY_FILE_UPLOAD_DATA_COMPLETE,false,false,event.data));
            }else if(event.data.search("<error>")==-1)
            {
            	dispatchEvent(new DataEvent(SINGLY_FILE_UPLOAD_DATA_COMPLETE,false,false,UploadInfoList.RESULT_ERROR));
            }
            else
            {
            	dispatchEvent(new DataEvent(SINGLY_FILE_UPLOAD_DATA_COMPLETE,false,false,UploadInfoList.RESULT_FAILURE));
            	updateUIElements(0,
            					 "uploadFailure",
            					 "browse");
            }
            
		}
		
		override public function updateUIElements(currentProgress:Number,
											progressBarLabel:String,
											buttonLabel:String,
											totalProgress:Number=100,
											progressBarVisible:Boolean=true,
											browseButtonEnable:Boolean=true,
											displayEnabled:Boolean=true):void
        {
        	if(progressBar)
        	{
        		progressBar.setProgress(currentProgress,totalProgress);
        		progressBar.visible = progressBarVisible;
        	}
        	
        	if( progressBar && progressBarLabel!=null)
        	{
//        		progressBar.label = progressBarLabel;
        		progressBar.label = "";
        	}
            
            if( browseButton && buttonLabel!=null)
            {
            	browseButton.label = buttonLabel;
            	browseButton.enabled = browseButtonEnable;
            }
            if( progressBar )
            {
            	progressBar.parentDocument.enabled = displayEnabled;
            }
        }
		
	}
}